<template>
  <view class="diy_register">
      <view v-if="$check_register_field('add', 'student_name', '/student_users/view')" class="diy_register_field col-md-6">
      <view class="diy_title">
        <text> 学生姓名 </text>
      </view>
                  <!-- 文本 -->
        <view class="diy_field diy_text">
          <input type="text" id="form_student_name" v-model="form['student_name']" @blur="handleBlur($event.target.value,'student_name')"  placeholder="请输入学生姓名" />
        </view>
              </view>
        <view v-if="$check_register_field('add', 'student_gender', '/student_users/view')" class="diy_register_field col-md-6">
      <view class="diy_title">
        <text> 学生性别 </text>
      </view>
          <!-- 选项 -->
      <view class="diy_field diy_down">
        <uni-data-select
                id="form_student_gender"
                v-model="form['student_gender']"
                :localdata="list_student_gender"
                @change="change_student_gender"
        ></uni-data-select>
      </view>
        </view>
        <view v-if="$check_register_field('add', 'student_age', '/student_users/view')" class="diy_register_field col-md-6">
      <view class="diy_title">
        <text> 学生年龄 </text>
      </view>
                  <!-- 文本 -->
        <view class="diy_field diy_text">
          <input type="text" id="form_student_age" v-model="form['student_age']" @blur="handleBlur($event.target.value,'student_age')"  placeholder="请输入学生年龄" />
        </view>
              </view>
        <view v-if="$check_register_field('add', 'student_id', '/student_users/view')" class="diy_register_field col-md-6">
      <view class="diy_title">
        <text> 学生学号 </text>
      </view>
                  <!-- 文本 -->
        <view class="diy_field diy_text">
          <input type="text" id="form_student_id" v-model="form['student_id']" @blur="handleBlur($event.target.value,'student_id')"  placeholder="请输入学生学号" />
        </view>
              </view>
    </view>
</template>

<script>
        import { uploadFilePathApi, uploadFileApi } from '@/api/common.js';
export default {
  model: {
    prop: 'form',
    event: 'change',
  },
  props: {
    form: {
      type: Object,
      default: () => {
        return {
          url_upload: '~/api/student_users/upload?',
        };
      },
    },
  },
  data() {
    return {
              "list_student_gender": [],
              };
  },
  methods: {
        /**
     * 获取学生性别列表
     */
    async get_list_student_gender() {
          ['男','女'].map((o) => this.list_student_gender.push({value:o,text:o}));
            },
    change_student_gender(val) {
      if (val === "全部" || val === "") {
        this.$emit('change',{"value":"","type":"student_gender"})
      }else{
        this.$emit('change',{"value":val,"type":"student_gender"})
      }
            },
          /**
     * 上传图片
     * @param {Object} param文件参数
     */
    change_img(key_name) {
      var _self = this;
      _self.upload_img_flag = false;
      // 选择图像方法
      uni.chooseImage({
        count: 1,
        sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
        sourceType: ['album'], //从相册选择
        success: (res) => {
          console.log('开始上传图片');
          console.log(tempFilePaths);
          const tempFilePaths = res.tempFilePaths;
          const filePath = tempFilePaths[0];
          uploadFilePathApi(
                  undefined,
                  filePath,
                  undefined,
                  {
                    i_want_to_customize: 'test',
                  },
                  (task) => {
                    task.onProgressUpdate((res) => {
                      this.percent = res.progress;
                      console.log('上传进度' + res.progress);
                      console.log('已经上传的数据长度' + res.totalBytesSent);
                      console.log('预期需要上传的数据总长度' + res.totalBytesExpectedToSend);
                    });
                  }
          ).then((res) => {
            const filename = res.result.url;
            _self.$delete(this.form, key_name);
            _self.$set(this.form, key_name, filename);
            _self.handleBlur(filename, key_name);
          });
        },
        error: function (e) {
          console.log(e);
        },
      });
    },
    handleBlur(value, type) {
      this.$emit('change', { value, type });
    },
  },
  mounted() {
          this.get_list_student_gender();
              },
};
</script>
